package src;


public class Tree {

    private Node root;

    public Tree(String value) {
        root = new Node(value);
    }

    public Node getRoot() {
        return root;
    }

    public Node addNode(Node root, Direction where, String value) {
        if (root == null) {
            return null;
        }

        if (where == Direction.LEFT) {

            if (root.left != null) {
                return root;
            }
            root.left = new Node(value);
            return root.left;
        } else {
            if (root.right != null) {
                return root;
            }
            root.right = new Node(value);
            return root.right;
        }
    }

    public int getHeight() {
        if (root.left == null && root.right == null) {
            return 0;
        } else {
            return getHeightRecursively(root, 0);
        }
    }

    public int getHeightRecursively(Node currentNode, int maxLevel) {

        int maxHeightFromLeftBranch;
        int maxHeightFromRightBranch;

        if (currentNode == null) {
            return maxLevel;
        }

        if (currentNode.left == null) {
            maxHeightFromLeftBranch = maxLevel;
        } else {
            maxHeightFromLeftBranch = getHeightRecursively(currentNode.left, maxLevel + 1);
        }

        if (currentNode.right == null) {
            maxHeightFromRightBranch = maxLevel;
        } else {
            maxHeightFromRightBranch = getHeightRecursively(currentNode.right, maxLevel + 1);
        }

        return Math.max(maxHeightFromLeftBranch, maxHeightFromRightBranch);
    }

    public String toString() {
        return "";
    }
}